package com.mysoft.gfyx.clue.controller;

import com.mysoft.framework.modeling.dataapi.OptionQueryDataApi;
import com.mysoft.framework.modeling.dto.OptionItem;
import com.mysoft.framework.modeling.dto.QueryOption;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.configuration.feign.ResponseDTO;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.service.ClueService;
import com.mysoft.gfyx.clue.mip.FuzzySearchService;
import com.mysoft.gfyx.clue.service.CustomerService;
import com.mysoft.gfyx.clue.mip.dto.FuzzySearchReqDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

@PubService(value = "/searchBusinessInfoOptionQuery", prefix = RequestPrefix.API)
@Tag(name = "查询工商信息-自动完成-API")
public class SearchBusinessInfoOptionQueryController extends com.mysoft.framework.mvc.api.Controller implements OptionQueryDataApi {
    @Autowired
    private CustomerService customerService;

    @Autowired
    private ClueService clueService;

    @Autowired
    private FuzzySearchService fuzzySearchService;

    /**
     * 客户反哺
     */
    @PubAction(value = "/customerFeeding", method = RequestMethod.POST)
    public ResponseDTO customerFeeding(String customerGUID) {
        //1.查询主档案有没有这个客户（公司）
        //2.没有这个客户，则推送给主档案接口
        ResponseDTO r = new ResponseDTO();
        r.setMessage("成功");
        r.setSuccess(true);
        r.setResult("成功");
        return r;
    }

    @Override
    //有用到吗
    public List<OptionItem> load(String values) {
        System.out.println("老大景来........");
//        List<OptionItem> optionItemList = new ArrayList<OptionItem>();
//        OptionItem item = new OptionItem();
//        item.setDefault(true);
//        item.setText("显示值1");
//        item.setValue("实际值1");
//        optionItemList.add(item);
//
//        OptionItem item2 = new OptionItem();
//        item2.setDefault(false);
//        item2.setText("显示值2");
//        item2.setValue("实际值2");
//        optionItemList.add(item2);
//
//        return optionItemList;
        return null;
    }

    @Override
    public List<OptionItem> query(QueryOption option, Map urlParams) {
        String key = option.getKey();
        FuzzySearchReqDto fuzzySearchReqDto = new FuzzySearchReqDto();
        fuzzySearchReqDto.setKeyword(key);
        System.out.println("dto=====");
        System.out.println("dto====="+fuzzySearchReqDto);
        LinkedHashMap fuzzySearchResult = fuzzySearchService.fuzzySearch(fuzzySearchReqDto);
        ArrayList data = (ArrayList) fuzzySearchResult.get("data");

        System.out.println("fuzzySearchResult====="+fuzzySearchResult);
        System.out.println("data====="+data);
        List<OptionItem> optionItemList = new ArrayList<OptionItem>();
        if (data != null && data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                OptionItem item = new OptionItem();
                LinkedHashMap fuzzySearchItem = (LinkedHashMap) data.get(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                item.setText((String) fuzzySearchItem.get("supplier_name"));
                String creditCode = (String) fuzzySearchItem.get("credit_code");
                if (Objects.equals(creditCode, "")) {
                    continue;
                }
                item.setValue(creditCode);
                Map<String, Object> map = new HashMap<>();
                map.put("credit_code", fuzzySearchItem.get("credit_code"));
                item.setExtData(map);
                optionItemList.add(item);
            }
        }
        return optionItemList;

        //测试
        /*
        List<OptionItem> optionItemList = new ArrayList<OptionItem>();

        for (int i=0; i<=10; i++) {
            OptionItem item = new OptionItem();

            item.setDefault(false);
            item.setText("显示值"+i);
            item.setValue("实际值"+i);
            Map<String, Object> map = new HashMap<>();
            map.put("USCC","企业信用代码"+i);
            item.setExtData(map);

            optionItemList.add(item);
        }


        return optionItemList;*/
    }
}
